Dashboard Temp Share Shortlinks Frames API

HTMLify

Generate Permutations of an array.py
Views: 31 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def permuteDist(self, arr):
        res = []
        self.solve(0, arr, res)
        return res
    
    def solve(self, index, arr, res):
        if index == len(arr):
            res.append(arr[:])
            return
        
        for i in range(index, len(arr)):
            arr[index], arr[i] = arr[i], arr[index]
            self.solve(index + 1, arr, res)
            arr[index], arr[i] = arr[i], arr[index]